﻿using ModuleHeal.Component;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using UnityEngine;
using Utils;



namespace ModuleRoundSummary.Event
{
    public class RoundSummaryEvent
    {
        Coroutine coroutine;

        [PluginEvent(ServerEventType.WarheadDetonation)]
        public void OnWarheadDetonation(WarheadDetonationEvent ev)
        {
            coroutine = TimeCoroutine.Delay(300f, () =>
            {
                var alives = Player.GetPlayers().FindAll(e => e.IsAlive);
                if (alives.Count > 0)
                {
                    uint total = 30;
                    coroutine = TimeCoroutine.Loop(30, 1, () =>
                    {
                        string message = $"[<color=red>终局时刻</color>] 地面强制肃清剩余时间 {total}s";
                        MessageStringUtils.GenerateAdminMessage(message, out string output);
                        total--;
                        Server.SendBroadcast(output, 1);
                    },
                    ()=>
                    {
                        Player.GetPlayers().FindAll(e => e.IsAlive).ForEach(e => e.SetRole(PlayerRoles.RoleTypeId.Spectator, PlayerRoles.RoleChangeReason.Died));
                    });                
                }
            });
        }

        [PluginEvent(ServerEventType.RoundEnd)]
        public void OnRoundEnd(RoundEndEvent ev)
        { 
            TimeCoroutine.Stop(coroutine);
        }
    }
}
